package controller;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Books;
import pojo.PageBean;
import service.BookService;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
    public final BookService bookservice;
    public BookController(@Qualifier("bookServiceImpl") BookService bookservice) {
        this.bookservice = bookservice;
    }
    //查询全部的书籍
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books=bookservice.queryAllBook();
        model.addAttribute("list",books);
        return "allBook";
    }
//    跳到请求的界面
    @RequestMapping("/toAddPage")
    public String toAddPage(){
        return "addBook";
    }
//    增加一本书的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
         bookservice.addBook(books);
        return "redirect:/book/findBookByPage";
    }
//    删除一本书
    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookservice.delete(id);
        return "redirect:/book/findBookByPage";
    }
//    跳转到跟新界面
    @RequestMapping("/toUpdateBooks")
    public  String toUpdateBooks(int id,Model model){
        Books book = bookservice.queryBookByID(id);
        model.addAttribute("book",book);
        return "updateBook";
    }
//    更新界面进行修改,修改完毕重定向
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        bookservice.updateBook(books);
        return "redirect:/book/findBookByPage";
    }
//    模糊查询书籍
    @RequestMapping("/searchBook")
    public String searchBook(String keyword,Model model){
        //传递参数模糊查询
        return null;
    }
    @ResponseBody
    @RequestMapping("checkBook")
    public String checkBook(String bookName){
        String msg="";
        Books books = bookservice.queryBookByBookName(bookName);
        if (books!=null){
             msg="该书已经存在";
        }else {
             msg="";
        }
        return msg;
    }
    //分页进行查询
    @RequestMapping("/findBookByPage")
    public String findBookByPage(String currentPage,String rows,String keyword,Model model){
        //防止启动时数据为空
        if (currentPage==null || " ".equals(currentPage)){
            currentPage="1";
        }
        if (rows==null || " ".equals(rows)){
            rows="5";
        }
        if (keyword==null || " ".equals(keyword)){
            keyword="";
        }
        //首次登陆
        //将pageBean保存
        PageBean<Books> pageBean = bookservice.queryBookByKeyword(Integer.parseInt(currentPage), Integer.parseInt(rows), keyword, keyword);
        model.addAttribute("pb",pageBean);
        model.addAttribute("keyword",keyword);
        return "allBook";
    }
    //导出Excel表格
    @RequestMapping("/easyExcelServlet")
    public String EasyExcelServlet(String list){
        System.out.println(list);

        List<Serializable> books = Collections.singletonList(list);
        System.out.println(books);
        return "redirect:/book/findBookByPage";
    }
}